package com.qianyu.project.web.controller.nyx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qianyu.project.entity.SystemUser;
import com.qianyu.project.entity.WorkLogDetail;
import com.qianyu.project.mapper.WorkLogMapper;
import com.qianyu.project.service.ISystemUserService;
import com.qianyu.project.service.IWorkLogDetailService;
import com.qianyu.project.service.IWorkLogService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("NyxWorkLogController")
public class NyxWorkLogController {

    @Autowired
    private IWorkLogService workLogService;
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private IWorkLogDetailService workLogDetailService;

    @GetMapping("")
    public Result getList(@RequestParam Map<String,String> map){
        return ResultTool.success(workLogService.getList(map));
    }

    @GetMapping("/id")
    public Result getId(){
        return ResultTool.success(workLogService.getId());
    }

    @GetMapping("/org")
    public Result getOrgList(@RequestParam Map<String,String> map){
        return ResultTool.success(workLogService.getOrgList(map));
    }

    @GetMapping("/detail/{id}")
    public Result getDetail(@PathVariable String id){
        return ResultTool.success(workLogDetailService.getDetail(id));
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable String id){
        workLogService.removeByIds(Arrays.asList(id.split(",").clone()));
        return ResultTool.success();
    }

    @PostMapping("")
    public Result save(@RequestBody WorkLogDeteil workLogDeteil){
        workLogDeteil.getWorkLog().setFillUser(systemUserService.getOne(new QueryWrapper<SystemUser>().eq("user_realname",workLogDeteil.getWorkLog().getFillUser())).getId().toString());
        workLogService.save(workLogDeteil.getWorkLog(),workLogDeteil.getWorkLogDetails());
        return  ResultTool.success();
    }

    @PutMapping("")
    public Result update(@RequestBody WorkLogDeteil workLogDeteil){
        workLogDeteil.getWorkLog().setFillUser(systemUserService.getOne(new QueryWrapper<SystemUser>().eq("user_realname",workLogDeteil.getWorkLog().getFillUser())).getId().toString());
        workLogService.updateWorkLog(workLogDeteil.getWorkLog(),workLogDeteil.getWorkLogDetails());
        return  ResultTool.success();
    }


}
